package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.AppQnyMapper;
import com.eco.fanliapp.entity.AppQny;
import com.eco.fanliapp.service.IAppQnyService;
import com.eco.fanliapp.utils.Convert;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 七牛云 服务层实现
 * 
 * @author xiaoxx
 * @date 2019-05-05
 */
@Service
public class AppQnyServiceImpl extends ServiceImpl<AppQnyMapper, AppQny> implements IAppQnyService {
	@Autowired
	private AppQnyMapper appQnyMapper;
	@Autowired
	private RedisTemplate redisTemplate;
	/**
     * 查询七牛云信息
     *
     * @return 七牛云信息
     */
    @Override
	public AppQny selectAppQnyById(Integer adminId)
	{
		AppQny appQny= null;
		if(redisTemplate.hasKey("AppQnyById")){
			appQny = (AppQny)redisTemplate.opsForValue().get("AppQnyById");
		}else {
			appQny = appQnyMapper.selectAppQnyById(adminId);
			redisTemplate.opsForValue().set("AppQnyById",appQny);
		}
	    return appQny;
	}
	
	/**
     * 查询七牛云列表
     * 
     * @param appQny 七牛云信息
     * @return 七牛云集合
     */
	@Override
	public List<AppQny> selectAppQnyList(AppQny appQny)
	{
		List<AppQny> list= null;
		if(redisTemplate.hasKey("AppQnyList")){
			list = (List<AppQny>)redisTemplate.opsForValue().get("AppQnyList");
		}else {
			list = appQnyMapper.selectAppQnyList(appQny);
			redisTemplate.opsForValue().set("AppQnyList",list);
		}
	    return list;
	}
	
    /**
     * 新增七牛云
     * 
     * @param appQny 七牛云信息
     * @return 结果
     */
	@Override
	public int insertAppQny(AppQny appQny)
	{
	    return appQnyMapper.insertAppQny(appQny);
	}
	
	/**
     * 修改七牛云
     * 
     * @param appQny 七牛云信息
     * @return 结果
     */
	@Override
	public int updateAppQny(AppQny appQny)
	{
	    return appQnyMapper.updateAppQny(appQny);
	}

	/**
     * 删除七牛云对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAppQnyByIds(String ids)
	{
		return appQnyMapper.deleteAppQnyByIds(Convert.toStrArray(ids));
	}

	@Override
	public AppQny selectAppQnyById(int userId) {
		AppQny appQny = appQnyMapper.selectAppQnyById(userId);
		return appQny ;
	}

	@Override
	public AppQny getOne() {

		AppQny appQny= null;
		if(redisTemplate.hasKey("AppQny")){
			appQny = (AppQny)redisTemplate.opsForValue().get("AppQny");
		}else {
			List<AppQny> appQnies =  appQnyMapper.selectAppQnyList(null);
			if(CollectionUtils.isNotEmpty(appQnies)){
				appQny =  appQnies.get(0);
			}
			redisTemplate.opsForValue().set("AppQny",appQny);
		}
		return appQny;

	}


}
